/**
 * 
 */
package com.armail.client.controller.impl;

import java.util.List;

import javax.ws.rs.core.MediaType;

import com.armail.client.controller.SentItemsController;
import com.armail.client.di.Context;
import com.armail.client.dto.Mail;
import com.armail.client.view.SentItemsView;
import com.armail.client.view.SentItemsViewListener;
import com.google.inject.Inject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

/**
 * @author arpit
 *
 */
public class SentItemsControllerImpl implements SentItemsController,
		SentItemsViewListener {
	
	private final SentItemsView sentItemsView;
	private static final String REST_URI = "http://localhost:8080/com-armail-server/";
	private static final String GET_SENT_ITEMS = "/mails/getSentItems/";

	@Inject
	public SentItemsControllerImpl(SentItemsView sentItemsView) {
		this.sentItemsView = sentItemsView;
	}

	public List<Mail> getSentItems() {
		ClientConfig config = new DefaultClientConfig();  
        Client client = Client.create(config);  
        WebResource resource = client.resource(REST_URI);  
        
        WebResource employeeResource = resource.path("/rest/").path(GET_SENT_ITEMS).path(Context.getUser().getId()+""); 
        final ClientResponse response = employeeResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
        return response.getEntity(new GenericType<List<Mail>>() {});
	}
	
	@Override
	public void activate() {
		sentItemsView.setModel(getSentItems());
		sentItemsView.attach();
	}

	@Override
	public void deactivate() {
		sentItemsView.detach();
	}

}
